

class CountImgCode {
    constructor(props) {
        this.config = this.objectAssign(props);
        this.canvas = null;
        this.code = '';
    }

    // 创建
    create = _ => {
        // canvas 不存在时创建canvas
        if(!this.canvas){
            this.canvas = document.createElement("canvas");
            this.canvas.id = 'CountImgCode';
            this.canvas.width = this.config.width;
            this.canvas.height = this.config.height;
        }

        // 判定 canvas 兼容性
        if(!this.canvas.getContext){
            return {
                imgUrl : '',
                code : 500,
                message : '您的浏览器版本不支持canvas！',
                imgCode : '',
            };
        }

        // 生成算术
        let valCode = this.randCode(this.config.str);
        //设置显示区域
        let fontsize = this.config.height - 6;
        this.config.width = 5 + fontsize * valCode.str.length;
        this.canvas.style.width = this.config.width;
        //设置画板
        this.canvas.setAttribute("width", this.config.width);
        this.canvas.setAttribute("height", this.config.height);
        //设置画笔
        let pen = this.canvas.getContext("2d");
        //绘制背景
        pen.fillStyle = this.randColor();
        pen.fillRect(0, 0, this.config.width, this.config.height);
        //设置水平线位置
        pen.textBaseline = "top";
        //绘制内容
        for (let i=0; i<valCode.str.length; i++){
            pen.fillStyle = this.randColor();
            pen.font = "bold " + (fontsize + this.randInt(3)) + "px 微软雅黑";
            pen.fillText(valCode.str.charAt(i), 5 + fontsize * i, this.randInt(5));
        }
        // 生成base64
        console.log(this.canvas.toDataURL('image/jpg'));
        this.code = valCode.code;
        return {
            imgUrl : this.canvas.toDataURL('image/jpg'),
            code : 200,
            message : '创建成功！',
            imgCode : this.code,
        };
    };

    // 获得正确答案
    getCode = _ => {
        return this.code
    };

    // 刷新
    refresh = _ => {
        this.code = '';
        this.create();
    };

    // 验证
    submit = value => {
        let {code} = this;
        return Number(value) === code;
    };
}

// TODO 随机生成最大值不超过max的整数
CountImgCode.prototype.randInt = (max) => {
    return Math.floor(Math.random() * 100000 % max);
};

// TODO 生成算术随机数
CountImgCode.prototype.randNum = (max, min = null) => {
    let num = parseInt(10*Math.random(), 10);
    if(!min && num !== 0){
        return num;
    }
    if(min < num || num === 0){
        return CountImgCode.prototype.randNum(max, min);
    }
    return num;
};

// TODO 生成rgb随机颜色
CountImgCode.prototype.randColor = () => {
    let r = CountImgCode.prototype.randInt(256);
    let g = CountImgCode.prototype.randInt(256);
    let b = CountImgCode.prototype.randInt(256);
    return "rgb("+r+","+g+","+b+")";
};

// TODO 生成随机长度的字符串验证码
CountImgCode.prototype.randCode = (str) => {
    let one = CountImgCode.prototype.randNum(20);
    let two = CountImgCode.prototype.randNum(10, one);
    let op = str.charAt(CountImgCode.prototype.randInt(str.length));
    let code;
    if(op === '-'){
        code = one - two;
    }
    if(op === '+'){
        code = one + two;
    }
    if(op === '*'){
        code = one * two;
    }
    if(op === '/'){
        code = one * two;
    }
    return {
        str : "" + one + op + two + "=",
        code
    };
};

// TODO 合并配置
CountImgCode.prototype.objectAssign = (props) => {
    if(!props || typeof props !== 'object'){
        props = {}
    }
    let config = {
        width : 80,  // 图片宽度
        height : 24, // 图片高度
        str : "+-",  // 算术符（只能包含+-*/）
    };
    return Object.assign(config, props)
};

module.exports = CountImgCode;
